// pages/list/list.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: [{
                order_id: "order1",
                storeName: "味凯拉面（深大店）",
                seatTime: "2022-05-15 13:43",
                sumprice: 130,
                foodNum: 9,
                whetherPay: false,
                items: [{
                        dish_id: 1,
                        name: "大骨浓汤猪软骨拉面",
                        price: 41,
                        count: 1,
                        imgURL: 'http://localhost:8080/img/1.png'
                    },
                    {
                        foodId: "d0002",
                        name: "骨汤番茄肥牛拉面",
                        price: 41.00,
                        note: "一口弹牙小嫩牛，酸爽多汁大番茄。经典番茄肥牛口味，配以浓香骨汤，口感鲜美酸爽。",
                        count: 1,
                        imgURL: 'http://localhost:8080/img/2.png'
                    },
                    {
                        foodId: 3,
                        name: "味凯酸酸辣辣面",
                        price: 32.00,
                        note: "味凯招聘拉面！酸爽口味搭配蜜汁猪颈肉，配以酸辣鲜香骨汤，唇齿留香，酸爽劲爆！",
                        zan: 34,
                        count: 1,
                        imgURL: 'http://localhost:8080/img/3.png'
                    },
                    {
                        foodId: 4, //int 型
                        name: "麻辣牛肉拉面", //菜名
                        price: 43.00, //价格
                        note: "精选麻辣牛肉浇头，浇以麻辣红汤，辛香升级、香辣过瘾。", //菜品描述
                        count: 1, //菜品点击数，初始化为0
                        imgURL: 'http://localhost:8080/img/4.png'
                    },
                    {
                        foodId: 5,
                        name: "潮汕叉烧拉面",
                        price: 32.30,
                        note: "潮汕叉烧风味，两分肥八分瘦。浓厚豚骨汤面，搭配厚切叉烧，浓香满足，大快朵颐。",
                        count: 1,
                        imgURL: 'http://localhost:8080/img/5.png'
                    },
                    {
                        foodId: 6,
                        name: "揭阳菌菇拉面",
                        price: 30.00,
                        note: "浓厚骨汤搭配清爽菌菇，解腻的同时带给你鲜美的口感。",
                        count: 1,
                        imgURL: 'http://localhost:8080/img/6.png'
                    }
                ]
            },
            {
                order_id: "order2",
                storeName: "味凯拉面（清华店）",
                seatTime: "2022-05-12 23:00",
                sumprice: 62,
                foodNum: 2,
                whetherPay: true,
                items: [{
                        foodId: 5,
                        name: "潮汕叉烧拉面",
                        price: 32,
                        note: "潮汕叉烧风味，两分肥八分瘦。浓厚豚骨汤面，搭配厚切叉烧，浓香满足，大快朵颐。",
                        count: 1,
                        imgURL: 'http://localhost:8080/img/5.png'
                    },
                    {
                        foodId: 6,
                        name: "揭阳菌菇拉面",
                        price: 30.00,
                        note: "浓厚骨汤搭配清爽菌菇，解腻的同时带给你鲜美的口感。",
                        count: 1,
                        imgURL: 'http://localhost:8080/img/6.png'
                    }
                ]
            }

        ],
        show: false,
        flag: [0, 0, 0], //每行标记变量 改变显示的五角星
        startext: ['', '', ''], //各个下标对应每行显示  5个评价中的一个 非常不满意/不满意
        stardata: [1, 2, 3, 4, 5], //每颗星星对应的数值  决定了渲染出几颗五角星
        kouwei: 0,
        fuwu: 0,
        huanjing: 0,
        comment: "",
        order_id: "",
    },
    // 五星评价事件
    changeColor: function (e) {
        var index = e.currentTarget.dataset.index; //判断选中了哪个评价 在前端页面写死的索引
        var num = e.currentTarget.dataset.no; //前端页面的 item 传来选择了多少个
        console.log(num)
        var a = 'flag[' + index + ']';
        var that = this;
        if (num == 1) {
            that.setData({
                [a]: 1,
            });
        } else if (num == 2) {
            that.setData({
                [a]: 2,
            });
        } else if (num == 3) {
            that.setData({
                [a]: 3,
            });
        } else if (num == 4) {
            that.setData({
                [a]: 4,
            });
        } else if (num == 5) {
            that.setData({
                [a]: 5,
            });
        }

    },



    click: function (res) {
        console.log(res.currentTarget.dataset.order_id);
        wx.setStorageSync('order_id', res.currentTarget.dataset.order_id);
        wx.navigateTo({
            url: '../list/order_detail/order_detail',
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var user_id = wx.getStorageSync('openid');
        console.log(user_id);
        var that = this;
        wx.request({
            url: 'http://localhost:8080/showHistoricalOrders?user_id=' + user_id,
            success: function (res) {
                console.log(res.data);
                that.setData({
                    order: res.data
                })
            }
        })
    },

    evaluate(res) {
        console.log(res);
        this.setData({
            show: !this.data.show,
            flag: [0, 0, 0],
            order_id: res.currentTarget.dataset.id,
        })
    },
    have_evaluate(){
        console.log(chenggong);
    },
    comment_sub(){
        var that = this;
        this.setData({
            kouwei: this.data.flag[0],
            fuwu: this.data.flag[1],
            huanjing: this.data.flag[2]
        })
        console.log("口味是："+this.data.kouwei);
        console.log("服务是："+this.data.fuwu);
        console.log("环境是："+this.data.huanjing);
        console.log("评价是："+this.data.comment);
        console.log("order_id是："+this.data.order_id);
        wx.request({
             url: 'http://localhost:8080/updateEvaluation?taste='+this.data.kouwei+'&order_id='+this.data.order_id+'&serve='+this.data.fuwu+'&environment='+this.data.huanjing+'&evaluation='+this.data.comment,
            data:{
                "kouwei" : this.data.kouwei,
                "fuwu" : this.data.fuwu,
                "huanjing" : this.data.huanjing,
                "order_id" : this.data.order_id,
                "comment" :this.data.comment
              },
              header: {
                "Content-Type": "application/json;charset=utf-8"
              },
              method: 'POST',
            success:function (res) {
                console.log(res);
                that.setData({
                    show: !that.data.show,
                })
                  wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration: 2000,    //提示的延迟时间
                  });
            }
          })


    },
    bindTextAreaBlur: function(e) {
        this.setData({
            comment: e.detail.value
        })
        console.log(this.data.comment);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading()
        var user_id = wx.getStorageSync('openid');
        console.log(user_id);
        var that = this;
        wx.request({
            url: 'http://localhost:8080/showHistoricalOrders?user_id=' + user_id,
            success: function (res) {
                console.log(res.data);
                that.setData({
                    order: res.data
                })
            }
        })
        setTimeout(() => {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        }, 400)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})